//
//  WordUtilities.m
//  Words2
//
//  Created by Mathieu on 10/07/06.
//  Copyright 2006 __MyCompanyName__. All rights reserved.
//

#import "WordUtilities.h"


@implementation WordUtilities

- (id) init {
	self = [super init];
	if (self != nil) {
		moc = [[NSApp delegate] managedObjectContext];
	}
	return self;
}

- (void) dealloc {

	[super dealloc];
}


// returns YES if the word exists already within the currently selected Application.
- (BOOL)wordHasBeenSeenBefore:(NSString *)aWord{
	NSLog(@"%@ is now being searched for", aWord);
	
	//this code is based on the fetching section of the core data programming guide.
	NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Word" 
														 inManagedObjectContext:moc];
	
	NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
	[request setEntity:entityDescription];
	
	// Set example predicate and sort orderings...
	NSPredicate *predicate = [NSPredicate predicateWithFormat:
		@"(word LIKE[c] '%@')", aWord];
	[request setPredicate:predicate];
	
	NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"word" 
																   ascending:YES];
	[request setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
	[sortDescriptor release];
	
	NSError *error = nil;
	NSArray *array = [moc executeFetchRequest:request error:&error];
	if (array == nil)
	{

	}
	
	if([array count] == 0){
		NSLog(@"Word %@ was not found!", aWord);
		return NO;
	}
	
	NSLog(@"Word %@ was found!", aWord);
	return YES;
	
	//create a fetch request to the word context for that LANGUAGE...
	//if there's nothing in the results array, 
}

@end
